<?
	use_db_wordpress();
	
	$tag_count = 0;
	$cat_count = 0;
	$tags_count = 0;

	$tag_check = @mysql_query("SELECT * FROM `$wp_tags`");
	$tag_check_rows = @mysql_num_rows($tag_check);
	
	if($tag_check_rows >= 1) {
		$query = @mysql_query("SELECT * FROM `$wp_posts`");
		while($row = @mysql_fetch_assoc($query)) { /* Move the Posts: Tags */
			$id = "";

			$id = $row['ID'];

			$tag_query = @mysql_query("SELECT * FROM `$wp_post2tag` WHERE `post_id`='$id'");
			while($row = @mysql_fetch_assoc($tag_query)) {
				$tag_id = "";
				$tag_id = $row['tag_id'];
				$tags_query = @mysql_query("SELECT * FROM `$wp_tags` WHERE `tag_id`='$tag_id'");
				while($row = @mysql_fetch_assoc($tags_query)) {
					$tag = "";
					$tag = $row['tag'];

					drizzo_db_connect();
					$tag_insert_query = @mysql_query("SELECT * FROM `$drizzo_posts` WHERE `import_id`='$id'");
					while($row = @mysql_fetch_assoc($tag_insert_query)) {
						$current_tags = "";
						$current_tags = $row['tags'];

						if(!empty($current_tags)) {
							$new_tags = $current_tags . ", " . $tag;							
						} else {
							$new_tags = $tag;
						}

						@mysql_query("UPDATE `$drizzo_posts` SET `tags`='$new_tags' WHERE `import_id`='$id'");
						
						$tags_count++;
						$tag_count++;
					}
					use_db_wordpress();
				}
			}
		}
	} else {
		echo "<p>No tag information was found.</p>";
	}

	use_db_wordpress();

	$query = @mysql_query("SELECT * FROM `$wp_posts`");
	while($row = @mysql_fetch_assoc($query)) { /* Move the Posts: Categories */
		$id = "";
		$status = "";
		$id = $row['ID'];
		$status = $row['post_status'];

		use_db_wordpress();

		if($status != "static") {
			$cat_query = @mysql_query("SELECT * FROM `$wp_post2cat` WHERE `post_id`='$id'");
			while($row = @mysql_fetch_assoc($cat_query)) {
				$cat_id = "";
				$cat_id = $row['category_id'];

				use_db_wordpress();

				$cats_query = @mysql_query("SELECT * FROM `$wp_categories` WHERE `cat_id`='$cat_id'");
				while($row = @mysql_fetch_assoc($cats_query)) {
					$category = "";
					$category = $row['category_nicename'];

					drizzo_db_connect();

					$cat_insert_query = @mysql_query("SELECT * FROM `$drizzo_posts` WHERE `import_id`='$id'");
					while($row = @mysql_fetch_assoc($cat_insert_query)) {
						$current_tags = "";
						$current_tags = $row['tags'];
						if(!empty($current_tags)) {
							$new_tags = $current_tags . ", " . $category;							
						} else {
							$new_tags = $category;
						}

						@mysql_query("UPDATE `$drizzo_posts` SET `tags`='$new_tags' WHERE `import_id`='$id'");

						$tags_count++;
						$cat_count++;
					}
				}
			}
		}
	}
	
	echo "<p>" . $cat_count . " category assignments have been converted to tags.</p>";

	echo "<p>" . ($cat_count + $tag_count) . " categories ";
	if($tag_count > 0) { echo "and tags "; }
	echo "have been imported.</p>";
?>
